<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { deptProfitReportByDate} from "@/api/dashboard";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      queryParams: {},
      daterangeCheckTime:[],
      chartData: {}
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },created() {

  },
  mounted() {
    this.$nextTick(() => {
      deptProfitReportByDate().then(response => {
        this.initChart(response.data);
      });

    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart(data) {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(data)
    },
    setOptions(data) {
      this.chart.setOption({
        title: {
          text: '本月门店盈亏状况',
          subtext: data.dataDate,
          x: 'center',
          y: 'top'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            crossStyle: {
              color: '#999'
            }
          }
        },
        legend: {
          data: ['月销量', '盈亏', '月销量折线'],
          x: 'center',
          y: 'bottom'
        },
        xAxis: [
          {
            type: 'category',
            data: data.deptList,
            axisPointer: {
              type: 'shadow'
            }
          }
        ],
        yAxis: [
          {
            type: 'value',
            name: '月销量',
            axisLabel: {
              formatter: '{value} ￥'
            }
          },
          {
            type: 'value',
            name: '盈亏',
            axisLabel: {
              formatter: '{value} ￥'
            }
          }
        ],
        series: [
          {
            name: '月销量',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ￥';
              }
            },
            data: data.salesList
          },
          {
            name: '盈亏',
            type: 'bar',
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ￥';
              }
            },
            data: data.profitList
          },
          {
            name: '月销量折线',
            type: 'line',
            yAxisIndex: 1,
            tooltip: {
              valueFormatter: function (value) {
                return value + ' ￥';
              }
            },
            data: data.salesList
          }
        ]
      })
    }
  }
}
</script>
